package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 36.有效的数独
 * <p>
 * 判断一个 9x9 的数独是否有效。只需要根据以下规则，验证已经填入的数字是否有效即可。
 * <p>
 * 数字 1-9 在每一行只能出现一次。
 * 数字 1-9 在每一列只能出现一次。
 * 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
 * <p>
 * <p>
 * 上图是一个部分填充的有效的数独。
 * <p>
 * 数独部分空格内已填入了数字，空白格用 '.' 表示。
 * <p>
 * 示例 1:
 * <p>
 * 输入:
 * [
 * ["5","3",".",".","7",".",".",".","."],
 * ["6",".",".","1","9","5",".",".","."],
 * [".","9","8",".",".",".",".","6","."],
 * ["8",".",".",".","6",".",".",".","3"],
 * ["4",".",".","8",".","3",".",".","1"],
 * ["7",".",".",".","2",".",".",".","6"],
 * [".","6",".",".",".",".","2","8","."],
 * [".",".",".","4","1","9",".",".","5"],
 * [".",".",".",".","8",".",".","7","9"]
 * ]
 * 输出: true
 * 示例 2:
 * <p>
 * 输入:
 * [
 *   ["8","3",".",".","7",".",".",".","."],
 *   ["6",".",".","1","9","5",".",".","."],
 *   [".","9","8",".",".",".",".","6","."],
 *   ["8",".",".",".","6",".",".",".","3"],
 *   ["4",".",".","8",".","3",".",".","1"],
 *   ["7",".",".",".","2",".",".",".","6"],
 *   [".","6",".",".",".",".","2","8","."],
 *   [".",".",".","4","1","9",".",".","5"],
 *   [".",".",".",".","8",".",".","7","9"]
 * ]
 * 输出: false
 * 解释: 除了第一行的第一个数字从 5 改为 8 以外，空格内其他数字均与 示例1 相同。
 * 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
 * 说明:
 *
 * @author: cuihao
 * @create: 2020-07-19 03:57
 **/
public class IsValidSudoku {

    /**
     * 优化方法，数组解法优化 推荐
     *
     * @param board
     * @return
     */
    public boolean isValidSudoku1(char[][] board) {
        // 第一个是行或列，第二个是数字，值为出现的次数，大于1表示出现两次了
        int[][] row = new int[9][10];
        int[][] col = new int[9][10];
        int[][][] cell = new int[3][3][10];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c == '.') {
                    continue;
                }
                System.out.println();
                int index = c - '0'; // 转成数字
                row[i][index]++;
                col[j][index]++;
                cell[i/3][j/3][index]++;

                if (row[i][index] > 1
                        || col[j][index] > 1
                        || cell[i/3][j/3][index] > 1
                ) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 使用map
     *
     * @param board
     * @return
     */
    public boolean isValidSudoku(char[][] board) {
        Map rowMap = new HashMap();
        Map[] columnMap = new HashMap[9];
        Map[] maps = new HashMap[9];

        for (int i = 0; i < 9; i++) {
            columnMap[i] = new HashMap();
            maps[i] = new HashMap();
        }

        for (int i = 0; i < board.length; i++) {
            rowMap.clear();
            for (int j = 0; j < board.length; j++) {
                char a = board[i][j];

                if (a == '.') {
                    continue;
                }
                Map map = maps[(i / 3) * 3 + j / 3];
                if (map.containsKey(a)) {
                    return false;
                } else {
                    map.put(a, null);
                }

                if (rowMap.containsKey(a)) {
                    return false;
                } else {
                    rowMap.put(a, null);
                }

                Map col = columnMap[j];
                if (col.containsKey(a)) {
                    return false;
                } else {
                    col.put(a, null);
                }
            }
        }
        return true;
    }
}
